﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.ApplicationServices
{
    public class CategoryService : ICategoryService
    {
        private readonly IRepository<Category> _categoryRepository;

        public CategoryService(IRepository<Category> categoryRepository)
        {
            Check.Require(categoryRepository != null);

            this._categoryRepository = categoryRepository;
        }

        /// <summary>
        /// Helps view all categories on list view
        /// </summary>
        /// <returns>List of all categories</returns>
        public IList<Category> GetAllCategories()
        {
            var categories = _categoryRepository.List();

            if (categories == null)
                categories = new List<Category>();

            Check.Ensure(categories != null);

            return categories.ToList();
        }

        /// <summary>
        /// Helps view categories for search view
        /// </summary>
        /// <param name="where">Lambda condition to get categories</param>
        /// <returns></returns>
        public IList<Category> GetCategories(Expression<Func<Category, bool>> @where)
        {
            var categories = _categoryRepository.List(@where);

            if (categories == null)
                categories = new List<Category>();

            Check.Ensure(categories != null);

            return categories.ToList();
        }

        /// <summary>
        /// Helps view active categories only
        /// </summary>
        /// <returns>All active categories</returns>
        public IList<Category> GetAllActiveCategories()
        {
            var categories = _categoryRepository.List(x => x.IsActive);

            if (categories == null)
                categories = new List<Category>();

            Check.Ensure(categories != null);

            return categories.ToList();
        }

        /// <summary>
        /// Helps view details of an category
        /// </summary>
        /// <param name="id">Identity of category</param>
        /// <returns>Category with specified identity</returns>
        public Category GetCategory(int id)
        {
            Check.Require(id > 0);

            Category category = _categoryRepository.Get(id);

            return category;
        }

        /// <summary>
        /// Insert an category from view
        /// </summary>
        /// <param name="category">Category to be inserted</param>
        /// <returns>Number of records affected</returns>
        public int InsertCategory(Category category)
        {
            Check.Require(category != null);

            _categoryRepository.Add(category);
            return _categoryRepository.SaveChanges();
        }

        /// <summary>
        /// Update an category from view
        /// </summary>
        /// <param name="category">Category to be updated</param>
        /// <returns>Number of records affected</returns>
        public int UpdateCategory(Category category)
        {
            Check.Require(category != null);

            _categoryRepository.Update(category);
            return _categoryRepository.SaveChanges();
        }

        /// <summary>
        /// Helps to inactive an category
        /// </summary>
        /// <param name="category">Category to be inactive</param>
        /// <returns>Number of records affected</returns>
        public int DeActivateCategory(Category category)
        {
            Check.Require(category != null);

            category.IsActive = false;
            _categoryRepository.Update(category);
            return _categoryRepository.SaveChanges();
        }
    }
}
